<template>
  <div class="search-playlist">
    <div class="count">找到{{playlistscount}}个歌单</div>
    <div class="playlist">
      <playlistitem class="playlist-item" :playitem="item" v-for="(item,index) in  playlists" :key="index" />
    </div>
    <div v-show="playlistscount>=100" class="pageset">
      <el-pagination :page-size="100" :current-page.sync="curPage" @current-change="getsearchPlaylist()"
        layout="prev, pager, next" :total="playlistscount" background></el-pagination>
    </div>
  </div>
</template>
<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getsearchAPI } from "@/api/search";
import playlistitem from "@/components/playlist-item/index";
export default {
  name: "search-song",
  //import引入的组件需要注入到对象中才能使用
  components: { playlistitem },
  props: {
    searchText: {
      require: true,
    },
  },
  data() {
    //这里存放数据
    return {
      curPage: 1,
      playlists: [],
      playlistscount: 0,
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    searchText() {
      this.getsearchPlaylist();
    },
  },
  //方法集合
  methods: {
    async getsearchPlaylist() {
      try {
        this.playlists = [];
        const { data } = await getsearchAPI(
          this.searchText,
          100,
          (this.curPage - 1) * 100,
          1000
        );
        // console.log(data);
        this.playlistscount = data.result.playlistCount;
        this.playlists = data.result.playlists;
        // 页面滚动优化用户体验
        if (document.documentElement.scrollTop != 0) {
          document.documentElement.scrollTop = 0;
        }
      } catch (error) {
        console.log(error + "获取歌单获取歌曲失败");
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getsearchPlaylist();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style scoped lang='less'>
.playlist {
  display: flex;
  flex-wrap: wrap;
  .playlist-item {
    margin-left: 30px;
    margin-right: 30px;
    margin-top: 20px;
  }
}
.pageset {
  display: flex;
  justify-content: center;
  .el-pagination {
    margin-top: 50px;
    margin-bottom: 140px;
  }
}
.count {
  display: inline-block;
  color: rgb(73, 73, 73);
}
</style>